package main

import (
	"context"
	"errors"
	"fmt"

	"gitee.com/wu-jin-feng/kinfu/fgin"
	reflectPb "gitee.com/wu-jin-feng/kinfu/fgin/demos/fhttpgrpc_test/pb"
	"github.com/gin-gonic/gin"
)

type DemoServiceServer struct {
	reflectPb.UnimplementedDemoServiceServer
}

func (s *DemoServiceServer) GetScheme(ctx context.Context, req *reflectPb.GetSchemeReq) (*reflectPb.GetSchemeResp, error) {
	if req.Info == nil {
		return nil, errors.New("info 不能为空")
	}
	endData := &reflectPb.GetSchemeResp{
		Data: req.Path + ",gogogo",
		Info: &reflectPb.GetSchemeRespInfo{
			Info: fmt.Sprintf("这是一个info,%s", req.Info.Info),
			Name: fmt.Sprintf("作者是%s", req.Info.Name),
		},
	}
	return endData, nil
}

type getSchemeInput struct {
	Path string `form:"path"`
	Info string `form:"info"`
	Name string `form:"name"`
}

func main() {
	f := &fgin.Fgin{}
	router, grpcServer := f.GetHttpGrpcServer("./http_grpc_cfg.yaml")
	reflectPb.RegisterDemoServiceServer(grpcServer, &DemoServiceServer{})
	router.GET("/public/get_scheme", func(ctx *gin.Context) {
		input := getSchemeInput{}
		if err := fgin.FbindParams(ctx, &input); err != nil {
			fgin.ReturnJson(ctx, 400, nil, err)
			return
		}
		server := &DemoServiceServer{}
		out, err := server.GetScheme(ctx, &reflectPb.GetSchemeReq{
			Path: input.Path,
			Info: &reflectPb.GetSchemeRespInfo{
				Info: input.Info,
				Name: input.Name,
			},
		})
		if err != nil {
			fgin.ReturnJson(ctx, 400, nil, err)
			return
		}
		fgin.ReturnJson(ctx, 200, out, nil)
	})
	f.RunHttpGrpc(router, grpcServer)
}
